package china.lzy.service.impl;

import china.lzy.dao.OrderMapper;
import china.lzy.entity.ResCode;
import china.lzy.entity.ResResult;
import china.lzy.entity.dto.PageDto;
import china.lzy.entity.pojo.Order;
import china.lzy.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lzy
 * @description order
 * @date 2021-08-16
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;


    @Override
    public ResResult insert(Order order) {

        // valid
        if (order == null || order.getShopId() == null) {
            return ResResult.ResResultBuilder(ResCode.PARAM_ERROR);
        }

        orderMapper.insert(order);
        return ResResult.ResResultBuilder(ResCode.OPERATE_OK);
    }


    @Override
    public ResResult delete(int id) {
        int ret = orderMapper.delete(id);
        return ret > 0 ? ResResult.ResResultBuilder(ResCode.OPERATE_ERROR) : ResResult.ResResultBuilder(ResCode.OPERATE_OK);
    }


    @Override
    public ResResult update(Order order) {
        int ret = orderMapper.update(order);
        return ret > 0 ? ResResult.ResResultBuilder(ResCode.OPERATE_ERROR) : ResResult.ResResultBuilder(ResCode.OPERATE_OK);
    }


    //TODO 单个查询SQL语句
    @Override
    public ResResult load(int id) {
        Order load = orderMapper.load(id);
        return ResResult.ResResultBuilder(load, ResCode.OPERATE_OK);
    }


    //TODO 分页查询SQL语句
    @Override
    public ResResult pageList(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        List<Order> orders = orderMapper.pageList();
        PageInfo pageInfo = new PageInfo<>(orders);
        pageDto.setTotal((int) pageInfo.getTotal());
        pageDto.setRecords(orders);
        return ResResult.ResResultBuilder(pageDto, ResCode.SAVE_OK);
    }
}